environment = new Env(); } public static function fromEnvironment(Env $environment) : self { $detector = new static(); $detector->environment = $environment; return $detector; } public function isCiDetected() : bool { $ciServer = $this->detectCurrentCiServer(); return $ciServer !== null; } public function detect() : CiInterface { $ciServer = $this->detectCurrentCiServer(); if ($ciServer === null) { throw new CiNotDetectedException('No CI server detected in current environment'); } return $ciServer; } /** * @return string[] */ protected function getCiServers() : array { return [Ci\AppVeyor::class, Ci\AwsCodeBuild::class, Ci\AzurePipelines::class, Ci\Bamboo::class, Ci\BitbucketPipelines::class, Ci\Buddy::class, Ci\Circle::class, Ci\Codeship::class, Ci\Continuousphp::class, Ci\Drone::class, Ci\GitHubActions::class, Ci\GitLab::class, Ci\Jenkins::class, Ci\SourceHut::class, Ci\TeamCity::class, Ci\Travis::class, Ci\Wercker::class]; } protected function detectCurrentCiServer() : ?CiInterface { $ciServers = $this->getCiServers(); foreach ($ciServers as $ciClass) { $callback = [$ciClass, 'isDetected']; if (\is_callable($callback) && $callback($this->environment)) { return new $ciClass($this->environment); } } return null; } }